<?php
include("dbconfig.php");
   header("Content-Type: text/html; charset=uft8");
   $link = mysql_connect($dbhost,$dbuser,$dbpassword) or die('Unable to establish a DB connection');

   mysql_select_db($database,$link);
   mysql_query("SET names UTF8");
   #$sql="select distinct(pid),name,id from job where cid=0 ";
   $sql="select * from job where pid='-1'";
   $result =mysql_query ($sql);
   if (!$result) echo "SQL错误0：".mysql_error();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf8" />
<title>职业管理</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/themes/black/jquery-ui-1.8.20.custom.css" />
<script src="js/jquery-1.7.1.js"></script>
<script src="js/ui/jquery.ui.core.js"></script>
<script src="js/ui/jquery.ui.widget.js"></script>	
<script type="text/javascript" src="js/ui/ui.formValidator.js"></script>
<script type="text/javascript" src="js/ui/jquery.ui.position.js"></script>
<script type="text/javascript" src="js/ui/jquery.ui.tooltip.js"></script>
<script type="text/javascript" src="js/func.js"></script>
<script type="text/javascript">
  function Jobs(array)
  {
   //数组，联动的数据源
  	this.array=array; 
  	this.indexName='';
  	this.obj='';
  	//设置子SELECT
	// 参数：当前onchange的SELECT ID，要设置的SELECT ID
      this.subSelectChange=function(selectName1,selectName2)
  	{
  	//try
  	//{
    var obj1=document.all[selectName1];
    var obj2=document.all[selectName2];
    var objName=this.toString();
    var me=this;
    obj1.onchange=function()
    {
    	me.optionChange(this.options[this.selectedIndex].value,obj2.id)
    }
  	}
  	//设置第一个SELECT
	// 参数：indexName指选中项,selectName指select的ID
  	this.firstSelectChange=function(indexName,selectName)  
  	{
  	this.obj=document.all[selectName];
  	this.indexName=indexName;
  	this.optionChange(this.indexName,this.obj.id)
  	}
  // indexName指选中项,selectName指select的ID
  	this.optionChange=function (indexName,selectName)
  	{
    var obj1=document.all[selectName];
    var me=this;
    obj1.length=0;
    obj1.options[0]=new Option("请选择",'');
    for(var i=0;i<this.array.length;i++)
    {	
    	if(this.array[i][1]==indexName)
    	{
    	//alert(this.array[i][1]+" "+indexName);
      obj1.options[obj1.length]=new Option(this.array[i][2],this.array[i][0]);
    	}
    }
  	}	
  }
	   function update(style)
	   {

	   if(style==0) {
	   var pid=$("#pid").val(); 
	           $.ajax({
	           url: 'job_manager.php?style='+style,
	           type: 'POST',
	           data:{pid:pid},
	           dataType:'html',
	           error: function(){alert("error!");},
	           success:function(result){alert(result);}
	             }); 	       
	       
	   }
	   else if(style==1){
	   var pid=$("#pct").val();
	   var cid=$("#cid").val();
	   	           $.ajax({
	           url: 'job_manager?style='+style,
	           type: 'POST',
	           data:{pid:pid,cid:cid},
	           dataType:'html',
	           error: function(){alert("error!");},
	           success:function(result){alert(result);}
	             }); 
	   
	   }
	   }
	   	   function del(style)
	   {

	   if(style==2) {
	   var dpid=$("#dpct").val(); 
	           $.ajax({
	           url: 'job_manager.php?style='+style,
	           type: 'POST',
	           data:{dpid:dpid},
	           dataType:'html',
	           error: function(){alert("error!");},
	           success:function(result){alert(result);}
	             }); 	       
	       
	   }
	   else if (style==3){
	   var dpid=$("#dpid").val();
	   var dcid=$("#dcid").val();
	   	           $.ajax({
	           url: 'job_manager?style='+style,
	           type: 'POST',
	           data:{dpid:dpid,dcid:dcid},
	           dataType:'html',
	           error: function(){alert("error!");},
	           success:function(result){alert(result);}
	             }); 
	   
	   }
	   }
</script>
</head>
<body>
<h1>职业管理</h1>

添加大类<input id="pid"></input><button onclick="update(0)" >确认</button>
</br>
添加小类<select id="pct">
<? while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {?>
    <option value="<?echo $row[cid];?>"><? echo $row[name];?></option>;
   <? }
?>
</select>
<input id="cid"></input><button onclick="update(1)" >确认</button>
</br>
<?$result =mysql_query ($sql);?>
删除大类
<select id="dpct">
<? while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {?>
    <option value="<?echo $row[cid];?>"><? echo $row[name];?></option>;
   <? }
?>
</select>
</input><button onclick="del(2)" >确认</button>
</br>
删除小类
<?
   $sql="select * from job";
   $result =mysql_query ($sql);
   if (!$result) echo "SQL错误0：".mysql_error();
  $res=array();
   while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
       array_push($res,array($row[cid],$row[pid],$row[name]));
    }
?>
 	<select id="dpid" name="dpid"  >
    <option selected></option>
  	</select>
  	<select id="dcid" name="dcid"  >
    <option selected></option>
  	</select>
<script>
var jobs='<?php echo json_encode($res); ?>';
jobs=eval(jobs);
  var liandong=new Jobs(jobs) //设置数据源
  liandong.firstSelectChange("-1","dpid"); //设置第一个选择框
  liandong.subSelectChange("dpid","dcid"); //设置子级选择框
</script>
<button onclick="del(3)" >确认</button>

</body>
</html>
